<?php
namespace app\vote\controller;
use think\Session;
class Wx extends \think\Controller
{

	//微信小程序登录处理
	public function xslogin(){
		$request=request()->param();
		$id=$request['account'];
		$pwd=$request['pwd'];
		$reuslt=db('user')->where(["userid"=>['=',"$id"],"password"=>['=',"$pwd"]])->find();
		if(isset($reuslt['userid'])){
			return json($reuslt); //需要转换为json格式传递回去
		}else{
			return json("empty");
		}
	}
	public function pwlogin(){
        $request=request()->param();
		$id=$request['account'];
		$pwd=$request['pwd'];
		$reuslt=db('judges')->where(["jid"=>['=',"$id"],"password"=>['=',"$pwd"]])->find();
		if(isset($reuslt['jid'])){
			return json($reuslt); //需要转换为json格式传递回去
		}else{
			return json("empty");
		}
	}

    public function addimg(){
        $request=request()->file('image');
        // $a=request()->param();
        $info =$request->rule('uniqid')->move('./static/vote/pics');
        // $time = time();  //获取当前时间戳
        // $date =  date('Ymd',time());  //时间戳转换为日期格式
        if($info){
            // $path="http://www.tp5.com/static/Ximg/$date/".$info->getFilename();

        // 成功上传后 获取上传信息
        $path="/pics/".$info->getFilename(); 
        return json("http://www.tp5.com/static/vote".$path); 
        }else{
            return "error";
        }
        // return json_encode($request) ;
    }
    public function xsenroll(){
        $request=request()->param();
		$username=$request['username'];
        $tel=$request['tel'];
		$pwd=$request['pwd'];
        $upic=$request['pic'];
        $data=['username'=>$username,'tel'=>$tel,'password'=>$pwd,'upic'=>$upic];
        $reuslt=db('user')->insertGetId($data);
        if($reuslt){
            return json($reuslt);
        }else{
            return "error";
        }
    }

    public function center(){
        $race=db('race')->select();
        return json($race);
    }

    public function getgroup(){
        $request=request()->param();
        $rid=$request['rid'];
        $group=db('playergroup')->distinct(true)->field('group')->where('rid',$rid)->select();
        return json($group);
    }
    public function getplayer(){
        $request=request()->param();
        $rid=$request['rid'];
        $group=$request['group'];
        $reuslt=db('playergroup')->where('rid',$rid)->where('group',$group)->select();
        return json($reuslt);
    }

    public function uservote(){
        $request=request()->param();
        $sid=$request['sid'];
        $xsid=$request['xsid'];
        $userid=$request['userid'];
        $hasvote=db('hasvote')->where('uid',$userid)->select();
        if($hasvote){
            return json("HASVOTE");
        }else{
                
                $update=db('vote')->where('xsid',$xsid)->where('sid',$sid)->find();
                if($update){
                    $count=(int)$update['num']+1;
                    $re=db('vote')->where('id',$update['id'])->update(['num'=>$count]);
                    db('hasvote')->insert(['uid'=>$userid]);
                    return json("VOTESUCCRSS");
                }else{
                    db('vote')->insert(['xsid'=>$xsid,'sid'=>$sid,'num'=>1]);
                    db('hasvote')->insert(['uid'=>$userid]);
                    return json("VOTESUCCRSS");
                }
               
        }
    }
    public function pwvote(){
        $request=request()->param();
        $sid=$request['sid'];
        $xsid=$request['xsid'];
        $pwid=$request['jid'];
        $score=$request['score'];

        $update=db('score')->where('xsid',$xsid)->where('sid',$sid)->where('pwid',$pwid)->find();
        if($update){
            $re=db('score')->where('id',$update['id'])->update(['score'=>$score]);

            $pw=db('score')->where('xsid',$xsid)->select();
            $cj=0;
            foreach($pw as $v){
                $cj=$cj+$v['score'];
            }
            $cj=$cj/count($pw)*0.6; //处理后的所有评委评价的成绩
            $user=db('vote')->where('xsid',$xsid)->find();
            $votes=(int)$user['num'];
            $uservote=$votes*0.4; //处理后的观众投票的票数得分
            $endscore=$cj+$uservote;
            db('player')->where('pid',$xsid)->update(['score'=>$endscore]);
            return json("VOTESUCCRSS");

        }else{
            db('score')->insert(['xsid'=>$xsid,'sid'=>$sid,'pwid'=>$pwid,'score'=>$score]);

            $pw=db('score')->where('xsid',$xsid)->select();
            $cj=0;
            foreach($pw as $v){
                $cj=$cj+$v['score'];
            }
            $cj=$cj/count($pw)*0.6; //处理后的所有评委评价的成绩
            $user=db('vote')->where('xsid',$xsid)->find();
            $votes=(int)$user['num'];
            $uservote=$votes*0.4; //处理后的观众投票的票数得分
            $endscore=$cj+$uservote;
            db('player')->where('pid',$xsid)->update(['score'=>$endscore]);

            return json("VOTESUCCRSS");
        }
    }

}   


